<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Horse races' );
?>
					<div class="blok">
<?php
printDiplomacyTab();
?> 
					</div>
					<br />
<?php
$race_team_id = -1;
$current_turn = getCurrentTurn();
if( isset( $_POST[ 'team_id' ] ) ) {
	$race_team_id = formatInput( $_POST[ 'team_id' ] );
}
if( is_numeric( $race_team_id ) ) {
	$query = "SELECT * FROM race_teams WHERE race_team_id = $race_team_id";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( isset( $row[ 'race_team_id' ] ) ) {
		
		$query2 = "SELECT * FROM races WHERE turn >= $current_turn ORDER BY turn DESC LIMIT 1";
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		if( isset( $row2[ 'race_id' ] ) ) {
			$morale = getMoraleString( $row[ 'morale' ] );
			
			$query2 = "SELECT count(*) FROM race_history WHERE race_team_id = $race_team_id";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			$races = $row2[ 'count(*)' ];
			
			if( $races > 0 ) {
				$query2 = "SELECT SUM(rank) FROM race_history WHERE race_team_id = $race_team_id";
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				$avg_rank = $row2[ 'SUM(rank)' ] / $races;
				
				$query2 = "SELECT count(*) FROM race_history WHERE race_team_id = $race_team_id AND rank = 1";
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				$won = round( $row2[ 'count(*)' ] / $races * 100.0 );
			}
			else {
				$avg_rank = "-";
				$won = "-";
			}
			
			$query2 = "SELECT SUM(gold_won) FROM race_history WHERE race_team_id = $race_team_id";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			$gold_won = number_format( $row2[ 'SUM(gold_won)' ] );
	?>
				<div class="blok">
					<?php transprint( 'Horse race team' ); ?>: <strong><?php print( $row[ 'name' ] ); ?></strong><br /><br />
					<?php transprint( 'Races' ); ?>: <?php print( number_format( $races ) ); ?><br />
					<?php transprint( 'Average rank' ); ?>: <?php print( round( $avg_rank, 2 ) ); ?><br />
					<?php transprint( '1st prize' ); ?>: <?php print( $won ); ?>%<br />
					<?php transprint( 'Prize gold' ); ?>: <strong class="gold"><?php print( $gold_won ); ?></strong><br /><br />
					<strong><?php transprint( 'Team attributes' ); ?></strong><br />
					<?php transprint( 'Acceleration' ); ?>: <?php print( $row[ 'acceleration' ] ); ?><br />
					<?php transprint( 'Agility' ); ?>: <?php print( $row[ 'agility' ] ); ?><br />
					<?php transprint( 'Intelligence' ); ?>: <?php print( $row[ 'intelligence' ] ); ?><br />
					<?php transprint( 'Stamina' ); ?>: <?php print( $row[ 'stamina' ] ); ?><br />
					<?php transprint( 'Strength' ); ?>: <?php print( $row[ 'strength' ] ); ?><br /><br />
					<?php transprint( 'Morale' ); ?>: <?php print( $morale ); ?><br />
					<br />
					<form method="post" action="submit_bet.php">
						<?php transprint( 'How much gold would you like to bet?' ); ?> <input type="text" name="gold" size="6" /><br />
						<?php transprint( 'Only bets over <strong class="gold">10</strong> gold and a maximum of <strong class="gold">100,000</strong> per team per race are allowed.' ); ?><br />
						<small><?php transprint( 'Note: The odds are lowered by 4%, 2% will be used for prizes and 2% for running costs.' ); ?></small><br />
						<br />
						<input type="submit" value="<?php transprint( 'Confirm bet' ); ?>" />
						<input type="hidden" name="team_id" value="<?php print( $race_team_id ); ?>" />
					</form>
				</div>
	<?php
		}
		else {
		?>
					<div class="blok">
						<?php transprint( 'There is no race scheduled yet.' ); ?>
					</div>
		<?php	
		}
	}
	else {
	?>
				<div class="blok">
					<?php transprint( 'Not a valid horse race team to bet on.' ); ?>
				</div>
	<?php	
	}
}
else {
	?>
				<div class="blok">
					<?php transprint( 'Not a valid horse race team to bet on.' ); ?>
				</div>
	<?php	
}
aos_footer();
?>